<%--
  Created by IntelliJ IDEA.
  User: carol
  Date: 2018/6/16
  Time: 20:33
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.sql.*,javax.servlet.*,functest.*"%>
<html>
<head>
    <link type="text/css" rel="stylesheet" href="layui/css/layui.css" media="all">
    <script type="text/javascript" src="layui/layui.all.js"></script>
    <meta charset="UTF-8">
</head>
<body>
<%
    FindAPrint func =new FindAPrint();
    request.setCharacterEncoding("UTF-8");
    String city_id = request.getParameter("cityid");
    String port_name = request.getParameter("portname");
    if (city_id == null){
        return;
    }
    else if(port_name == null) {
        String city_oldname = request.getParameter("cityoldname");
%>
    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
        <legend>添加机场</legend>
    </fieldset>
    <form class="layui-form" method="post" style="margin: 20px" action="addport.jsp">
        <div class="layui-form-item">
            <label class="layui-form-label">城市id</label>
            <div class="layui-input-block">
                <input type="text" name="cityid" readonly class="layui-input" value="<%=city_id%>">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">城市名称</label>
            <div class="layui-input-block">
                <input type="text" readonly class="layui-input" value="<%=city_oldname%>">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">机场名称</label>
            <div class="layui-input-block">
                <input type="text" name="portname" required="required" lay-verify="portname" placeholder="请输入" autocomplete="on" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item" style="text-align: center;">
            <button class="layui-btn" lay-submit="" lay-filter="portadd">确认提交</button>
        </div>
    </form>
    <%
    }
    else{
        String driver="com.mysql.jdbc.Driver";
        String url = "jdbc:mysql://localhost:3306/hbpw?useUnicode=true&characterEncoding=utf-8&useSSL=false";//地址
        String user="hbpwuser";
        String pass="LFFLXY";
        Class.forName(driver);                   //加载jdbc驱动
        Connection connection  = (Connection) DriverManager.getConnection(url,user,pass);

        Statement statement2 = (Statement) connection.createStatement();  //连接实例
        String selectsql2 = "select count(*) as checkname from jc where JC_MC='" + port_name + "' and CS_DM=" + city_id + ";";
        ResultSet rst2 = statement2.executeQuery(selectsql2);
        if (rst2.next()){
            int checkport = rst2.getInt("checkname");
            if (checkport > 0){
    %>
<blockquote class="layui-elem-quote layui-quote-nm">已有重名机场！</blockquote>
<%
}
else {
    Statement state = (Statement) connection.createStatement();  //连接实例
    String insertsql = "insert into jc(CS_DM,JC_MC) values(" + city_id + ",'" + port_name +"');";
    try {
        state.executeUpdate(insertsql);
%>
<blockquote class="layui-elem-quote layui-quote-nm">添加成功</blockquote>
<%
                }catch (SQLException e){
                    out.println(insertsql);
                    out.println(e);
                }
            }
        }
    }
%>
</body>
</html>
<script type="text/javascript">
    layui.use(['form','layer'],function (){
        var form = layui.form
            ,layer = layui.layer;
        form.verify({
            portname: function(value, item){ //value：表单的值、item：表单的DOM对象
                if(!new RegExp("^[a-zA-Z\u4e00-\u9fa5\\s·]+$").test(value)){
                    return '请输个正常的名称';
                }
            }
        });
        form.render();
    })
</script>
